package com.nineclock.doc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nineclock.common.entity.PageResult;
import com.nineclock.common.threadlocal.CurrentUserHolder;
import com.nineclock.common.utils.BeanHelper;
import com.nineclock.doc.dto.DocFileDto;
import com.nineclock.doc.mapper.CollaborationsMapper;
import com.nineclock.doc.mapper.DocFileMapper;
import com.nineclock.doc.pojo.DocCollaborations;
import com.nineclock.doc.pojo.DocFile;
import com.nineclock.doc.service.DocFileService;
import com.nineclock.system.dto.SysCompanyUserDTO;
import com.nineclock.system.feign.SysUserFeign;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class DocFileServiceImpl implements DocFileService {

    @Autowired
    private DocFileMapper docFileMapper;

    @Autowired
    private SysUserFeign userFeign;

    @Autowired
    private CollaborationsMapper collaborationsMapper;

    @Override
    public PageResult<DocFileDto> seeFilePagesList(Integer page, Integer pageSize, String keyword) {
         LambdaQueryWrapper<DocFile> wrapper = new LambdaQueryWrapper<>();

        if(keyword!=null) {
            wrapper.and(wrapperone->{
                //构建模糊查询模型
                wrapperone.like(DocFile::getContent,keyword).or()
               .like(DocFile::getName,keyword);
            });
        }
        wrapper.eq(DocFile::getCompanyId,CurrentUserHolder.get().getCompanyId());
            //分页查询,构建分页查询模型
            Page<DocFile> docFilePage = new Page<>(page,pageSize);

            docFilePage= docFileMapper.selectPage(docFilePage, wrapper);

            List<DocFile> records = docFilePage.getRecords();

        ArrayList<DocFileDto> docFiles = new ArrayList<>();
        List<SysCompanyUserDTO> data = userFeign.queryCompanyUser(null, CurrentUserHolder.get().getCompanyId()).getData();

        for (DocFile record : records) {
            DocFileDto docFileDto = new DocFileDto();

            docFileDto.setCompanyId(record.getCompanyId());
            docFileDto.setContent(record.getContent());
            docFileDto.setCreateTime(record.getCreateTime());
            docFileDto.setUpdateTime(record.getUpdateTime());
            docFileDto.setCreateUserId(record.getCreateUserId());

            for (SysCompanyUserDTO companyUserDTO : data) {
               if(companyUserDTO.getId()==record.getCreateUserId()) {

                   SysCompanyUserDTO userDTO = new SysCompanyUserDTO();

                   userDTO.setCompanyId(companyUserDTO.getCompanyId());
                   userDTO.setCompanyName(companyUserDTO.getDepartmentName());
                   userDTO.setDepartmentId(companyUserDTO.getDepartmentId());
                   userDTO.setDepartmentName(companyUserDTO.getDepartmentName());
                   userDTO.setUserId(companyUserDTO.getUserId());
                   userDTO.setUserName(companyUserDTO.getUserName());
                   docFileDto.setUpdateUserName(companyUserDTO.getUserName());
                   docFileDto.setCreator(userDTO);
                LambdaQueryWrapper<DocCollaborations> collaborationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
                collaborationsLambdaQueryWrapper.eq(DocCollaborations::getFileId,record.getId());
                List<DocCollaborations> docCollaborations = collaborationsMapper.selectList(collaborationsLambdaQueryWrapper);
                   ArrayList<SysCompanyUserDTO> sysCompanyUserDTOS = new ArrayList<>();
                   for (DocCollaborations docCollaboration : docCollaborations) {
                       if(companyUserDTO.getId()==docCollaboration.getCollaborationId()){

                           sysCompanyUserDTOS.add(companyUserDTO);
                           docFileDto.setCollaborations(sysCompanyUserDTOS);
                   }
                 }
               }
            }

            docFileDto.setFilePath(record.getFilePath());
            docFileDto.setFileSize(record.getFileSize());
            docFileDto.setFolderId(record.getFolderId());
            docFileDto.setId(record.getId());
            docFileDto.setName(record.getName());

            docFileDto.setType(record.getType());
            docFileDto.setUpdateUserId(record.getUpdateUserId());
            docFileDto.setPermission(record.getPermission());
            docFileDto.setStatus(record.getStatus());

            docFiles.add(docFileDto);
        }
            return new PageResult(docFilePage.getTotal(),docFilePage.getPages(),docFiles);
    }
}
